<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Index patterns | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'sql-index-patterns.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/sql-index-patterns.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/sql-index-patterns.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/sql-index-patterns.html" rel="nofollow" target="_blank">../en/sql-index-patterns.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-sql.html">SQL access</a></span>
»
<span class="breadcrumb-link"><a href="sql-spec.html">SQL Language</a></span>
»
<span class="breadcrumb-node">Index patterns</span>
</div>
<div class="navheader">
<span class="prev">
<a href="sql-data-types.html">« Data Types</a>
</span>
<span class="next">
<a href="sql-index-frozen.html">Frozen Indices »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="sql-index-patterns"></a>Index patterns<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/language/indices.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>Elasticsearch SQL supports two types of patterns for matching multiple indices or tables:</p>
<h4>
<a id="sql-index-patterns-multi"></a>Elasticsearch multi-index<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/language/indices.asciidoc">edit</a>
</h4>
<p>The Elasticsearch notation for enumerating, including or excluding <a class="xref" href="multi-index.html" title="Multiple indices">multi index syntax</a>
is supported <em>as long</em> as it is quoted or escaped as a table identifier.</p>
<p>For example:</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SHOW TABLES "*,-l*";

     name      |     type      |     kind
---------------+---------------+---------------
emp            |BASE TABLE     |INDEX
employees      |VIEW           |ALIAS</pre>
</div>
<p>Notice the pattern is surrounded by double quotes <code class="literal">"</code>. It enumerated <code class="literal">*</code> meaning all indices however
it excludes (due to <code class="literal">-</code>) all indices that start with <code class="literal">l</code>.
This notation is very convenient and powerful as it allows both inclusion and exclusion, depending on
the target naming convention.</p>
<p>The same kind of patterns can also be used to query multiple indices or tables.</p>
<p>For example:</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT emp_no FROM "e*p" LIMIT 1;

    emp_no
---------------
10001</pre>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>There is the restriction that all resolved concrete tables have the exact same mapping.</p>
</div>
</div>
<h4>
<a id="sql-index-patterns-like"></a>SQL <code class="literal">LIKE</code> notation<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/language/indices.asciidoc">edit</a>
</h4>
<p>The common <code class="literal">LIKE</code> statement (including escaping if needed) to match a wildcard pattern, based on one <code class="literal">_</code>
or multiple <code class="literal">%</code> characters.</p>
<p>Using <code class="literal">SHOW TABLES</code> command again:</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SHOW TABLES LIKE 'emp%';

     name      |     type      |     kind
---------------+---------------+---------------
emp            |BASE TABLE     |INDEX
employees      |VIEW           |ALIAS</pre>
</div>
<p>The pattern matches all tables that start with <code class="literal">emp</code>.</p>
<p>This command supports <em>escaping</em> as well, for example:</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SHOW TABLES LIKE 'emp!%' ESCAPE '!';

     name      |     type      |     kind
---------------+---------------+---------------</pre>
</div>
<p>Notice how now <code class="literal">emp%</code> does not match any tables because <code class="literal">%</code>, which means match zero or more characters,
has been escaped by <code class="literal">!</code> and thus becomes an regular char. And since there is no table named <code class="literal">emp%</code>,
an empty table is returned.</p>
<p>In a nutshell, the differences between the two type of patterns are:</p>
<div class="informaltable">
<table border="1" cellpadding="4px">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
</colgroup>
<tbody>
<tr>
<td align="center" valign="top"><p><span class="strong strong"><strong>Feature</strong></span></p></td>
<td align="center" valign="top"><p><span class="strong strong"><strong>Multi index</strong></span></p></td>
<td align="center" valign="top"><p><span class="strong strong"><strong>SQL <code class="literal">LIKE</code></strong></span></p></td>
</tr>
<tr>
<td align="center" valign="top"><p><span class="strong strong"><strong>Type of quoting</strong></span></p></td>
<td align="center" valign="top"><p><code class="literal">"</code></p></td>
<td align="center" valign="top"><p><code class="literal">'</code></p></td>
</tr>
<tr>
<td align="center" valign="top"><p><span class="strong strong"><strong>Inclusion</strong></span></p></td>
<td align="center" valign="top"><p>Yes</p></td>
<td align="center" valign="top"><p>Yes</p></td>
</tr>
<tr>
<td align="center" valign="top"><p><span class="strong strong"><strong>Exclusion</strong></span></p></td>
<td align="center" valign="top"><p>Yes</p></td>
<td align="center" valign="top"><p>No</p></td>
</tr>
<tr>
<td align="center" valign="top"><p><span class="strong strong"><strong>Enumeration</strong></span></p></td>
<td align="center" valign="top"><p>Yes</p></td>
<td align="center" valign="top"><p>No</p></td>
</tr>
<tr>
<td align="center" valign="top"><p><span class="strong strong"><strong>One char pattern</strong></span></p></td>
<td align="center" valign="top"><p>No</p></td>
<td align="center" valign="top"><p><code class="literal">_</code></p></td>
</tr>
<tr>
<td align="center" valign="top"><p><span class="strong strong"><strong>Multi char pattern</strong></span></p></td>
<td align="center" valign="top"><p><code class="literal">*</code></p></td>
<td align="center" valign="top"><p><code class="literal">%</code></p></td>
</tr>
<tr>
<td align="center" valign="top"><p><span class="strong strong"><strong>Escaping</strong></span></p></td>
<td align="center" valign="top"><p>No</p></td>
<td align="center" valign="top"><p><code class="literal">ESCAPE</code></p></td>
</tr>
</tbody>
</table>
</div>
<p>Which one to use, is up to you however try to stick to the same one across your queries for consistency.</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>As the query type of quoting between the two patterns is fairly similar (<code class="literal">"</code> vs <code class="literal">'</code>), Elasticsearch SQL <em>always</em>
requires the keyword <code class="literal">LIKE</code> for SQL <code class="literal">LIKE</code> pattern.</p>
</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="sql-data-types.html">« Data Types</a>
</span>
<span class="next">
<a href="sql-index-frozen.html">Frozen Indices »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>